﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmPhieuThuePhong : Form
    {
        public frmPhieuThuePhong()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPhieuThuePhong_Load(object sender, EventArgs e)
        {
            List<PhongInfo> dsPhongTrong = new List<PhongInfo>();
            dsPhongTrong = PhieuThuePhongBUS.DanhSachPhongTrong();
            dgvTrong.AutoGenerateColumns = false;
            foreach (PhongInfo p in dsPhongTrong)
            {
                dgvTrong.Rows.Add(p.MaPhong, p.TenPhong, p.LoaiPhong.Tenloaiphong, p.LoaiPhong.Gia, p.LoaiPhong.Soktd, p.MoTa);
            }

            List<PhieuThueInfo> dsPhieuThue = new List<PhieuThueInfo>();
            dsPhieuThue = PhieuThuePhongBUS.DanhSachPhieuThue();
            foreach(PhieuThueInfo p in dsPhieuThue)
            {
                dgvCoKhach.Rows.Add(p.MaPhieu, p.MaPhong, p.TenPhong, p.HoTen, p.LoaiPhong, p.NgayBD, p.NgayKT);
            }
        }

        private void btnPhieuMoi_Click(object sender, EventArgs e)
        {
            frmThemPhieuThuePhong frm = new frmThemPhieuThuePhong();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                for(int i=0 ;i<this.dgvTrong.Rows.Count-1;i++)
                {
                    if (dgvTrong.Rows[i].Cells["coldgvTrongMaPhong"].Value.ToString() == frm.MaPhong)
                    {
                        dgvTrong.Rows.RemoveAt(i);
                    }
                }

                PhieuThueInfo p = new PhieuThueInfo();
                p = PhieuThuePhongBUS.LayPhieuThuePhong(frm.MaPhieu);
                dgvCoKhach.Rows.Add(p.MaPhieu, p.MaPhong, p.TenPhong, p.HoTen, p.LoaiPhong, p.NgayBD, p.NgayKT);
            }
        }

        private void dgvTrong_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvTrong.Columns["coldgvTrongDatPhong"].Index)
            {
                string MaPhong = dgvTrong.Rows[e.RowIndex].Cells["coldgvTrongMaPhong"].Value.ToString();
                frmThemPhieuThuePhong frm = new frmThemPhieuThuePhong(MaPhong);
                frm.ShowDialog();
            }
        }

        private void dgvCoKhach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvCoKhach.Columns["colThanhToan"].Index)
            {
                int MaPhieu = Int32.Parse(dgvCoKhach.Rows[e.RowIndex].Cells["colMaPhieu"].Value.ToString());
                frmXuatHoaDon frm = new frmXuatHoaDon(MaPhieu);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    PhongInfo p = new PhongInfo();
                    p = PhieuThuePhongBUS.LayThongTinPhong(dgvCoKhach.Rows[e.RowIndex].Cells["colMaPhong"].Value.ToString());
                    this.dgvCoKhach.Rows.RemoveAt(e.RowIndex);
                    dgvTrong.Rows.Add(p.MaPhong, p.TenPhong, p.LoaiPhong.Tenloaiphong, p.LoaiPhong.Gia, p.LoaiPhong.Soktd, p.MoTa);
                }
            }
        }
    }
}
